import os
import sys
from pathlib import Path

def check_directory(path: Path):
    print(f"检查目录: {path.resolve()}")
    
    if not path.exists():
        print(f"错误: 目录不存在")
        return False
    
    test_file = path / '.permission_test'
    try:
        test_file.touch()
        test_file.write_text("test")
        test_file.unlink()
        print("✓ 读写权限正常")
        return True
    except PermissionError:
        print("✗ 无写权限")
        return False
    except Exception as e:
        print(f"✗ 未知错误: {str(e)}")
        return False

def main():
    required_dirs = [
        Path('./docs'),
        Path('./static'),
        Path('./templates')
    ]

    all_valid = True
    for d in required_dirs:
        if not check_directory(d):
            all_valid = False

    if not all_valid:
        print("\n请执行以下命令修复权限：")
        print(f"sudo chmod -R 755 {' '.join(str(d) for d in required_dirs)}")
        sys.exit(1)

    print("\n✅ 所有预检通过")

if __name__ == '__main__':
    main()